import axios from 'axios'
import { Message } from 'element-ui'

const strUserInfo = localStorage.getItem('userInfo')
let userInfo
try {
  userInfo = JSON.parse(strUserInfo)
} catch (error) {

}

const token = userInfo ? userInfo.token : ''
console.log(token)
// axios.defaults.timeout = 5000;
axios.defaults.baseURL = 'http://112.17.112.137:8888'; // 线上
// axios.defaults.baseURL = 'http://192.168.1.25:8888'      //
axios.defaults.headers = {
  Authorization: 'Bearer ' + token
  // Authorization: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiLotoXnuqfnrqHnkIblkZgiLCJqdGkiOiIxIiwiZmFtaWx5X25hbWUiOiJNYW5hZ2VyIiwibW9iaWxlIjoiIiwicGhvbmUiOiIiLCJkZXBhcnRtZW50Ijoi5Yy65Lq65aSnIiwiZXhwIjoxNTI3NDczMjQwLCJpc3MiOiJhYnUiLCJhdWQiOiJ3ZWIifQ.q9zK9_v7eHTXIe-H5NtkIBQVtFOJ8UahEhHHdDvXVHA'
}
// //http request 拦截器
// axios.interceptors.request.use(
//   config => {
//     // const token = getCookie('名称');注意使用的时候需要引入cookie方法，推荐js-cookie
//     config.data = JSON.stringify(config.data);
//     config.headers = {
//       'Content-Type': 'application/x-www-form-urlencoded'
//     }
//     // if(token){
//     //   config.params = {'token':token}
//     // }
//     return config;
//   },
//   error => {
//     return Promise.reject(err);
//   }
// );

axios.interceptors.response.use((response) => {
  const data = response.data
  return response
}, (err) => { // 这里是返回状态码不为200时候的错误处理
  if (err && err.response) {
    switch (err.response.status) {
      case 400:
        err.message = err.response.data
        break
      case 401:
        err.message = '未授权，请登录'
        break
      case 403:
        err.message = '拒绝访问'
        break
      case 404:
        err.message = `请求地址出错!`
        break
      case 408:
        err.message = '请求超时'
        break
      case 500:
        err.message = '服务器内部错误'
        break
      case 501:
        err.message = '服务未实现'
        break
      case 502:
        err.message = '网关错误'
        break
      case 503:
        err.message = '服务不可用'
        break
      case 504:
        err.message = '网关超时'
        break
      case 505:
        err.message = 'HTTP版本不受支持'
        break
      default:
    }
  }

  return Promise.reject(err)
})

/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */

export function get(url, params = {}) {
  return new Promise((resolve, reject) => {
    axios.get(url, {
      params: params
    })
      .then(response => {
        resolve(response.data)
      })
      .catch(err => {
        reject(err)
      })
  })
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.post(url, data)
      .then(response => {
        resolve(response.data)
      }, err => {
        reject(err)
      })
  })
}

/**
* 封装patch请求
* @param url
* @param data
* @returns {Promise}
*/

export function del(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.delete(url, data)
      .then(response => {
        resolve(response.data)
      }, err => {
        reject(err)
      })
  })
}

/**
* 封装put请求
* @param url
* @param data
* @returns {Promise}
*/

export function put(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.put(url, data)
      .then(response => {
        resolve(response.data)
      }, err => {
        reject(err)
      })
  })
}
